package com.baibei.ebec.welfare.rank;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.RaeTabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.baibei.ebec.home.BuildConfig;
import com.baibei.ebec.home.R;
import com.baibei.ebec.home.R2;
import com.baibei.ebec.sdk.ApiConstant;
import com.baibei.ebec.sdk.ApiFactory;
import com.baibei.ebec.welfare.fragment.WelfareRankingFragment;
import com.baibei.model.HelpContentInfo;
import com.baibei.module.AppRouter;
import com.baibei.sdk.ApiDefaultObserver;
import com.baibei.sdk.RxObservable;
import com.baibei.widget.CommonRefreshLayout;
import com.baibei.widget.SerializableRefreshLayout;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

@Route(path = AppRouter.ROUTE_WELFARE_RANK)
public class WelfareRankingActivity extends AppCompatActivity {

    @BindView(R2.id.iv_banner)
    ImageView ivBanner;
    @BindView(R2.id.refresh_layout)
    CommonRefreshLayout refreshLayout;
    @BindView(R2.id.tab_layout)
    RaeTabLayout tabLayout;
    @BindView(R2.id.view_pager)
    ViewPager viewPager;

    private String[] titles;
    private ArrayList<Fragment> fragments;
    private String helpContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welfare_ranking);
        ButterKnife.bind(this);
        init();
        refreshLayout.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout ptrFrameLayout) {
                viewPager.removeAllViews();
                tabLayout.removeAllTabs();
                init();
                refreshLayout.refreshComplete();
            }
        });
    }

    private void init() {

        titles = new String[]{"昨日盈利榜", "本周盈利榜", "本月盈利榜"};

        for (String title : titles){
            tabLayout.addTab(tabLayout.newTab().setText(title));
        }

        SerializableRefreshLayout serializableRefreshLayout = new SerializableRefreshLayout();
        serializableRefreshLayout.setRefreshLayout(refreshLayout);

        fragments = new ArrayList<>();
        fragments.add(WelfareRankingFragment.newInstance("day",serializableRefreshLayout));
        fragments.add(WelfareRankingFragment.newInstance("week",serializableRefreshLayout));
        fragments.add(WelfareRankingFragment.newInstance("month",serializableRefreshLayout));
        viewPager.setOffscreenPageLimit(fragments.size());
        viewPager.setAdapter(new FragmentAdapter(getSupportFragmentManager()));
        tabLayout.setupWithViewPager(viewPager);

        getHelpInfo();
    }

    private void getHelpInfo() {
        RxObservable.create(ApiFactory.getInstance().getTradeApi().getWelfareHelp(),"rank").subscribe(new ApiDefaultObserver<List<HelpContentInfo>>() {
            @Override
            protected void onError(String s) {

            }

            @Override
            protected void accept(List<HelpContentInfo> helpContentInfos) {
                helpContent = helpContentInfos.get(0).getText();
            }
        });
    }


    @OnClick({R2.id.iv_back, R2.id.iv_help})
    public void onViewClicked(View view) {
        if (view.getId() == R.id.iv_back) {
            finish();
        } else  {
            popHelpDialog();

        }
    }

    private void popHelpDialog() {
        Dialog helpDialog = new Dialog(this,R.style.HelpDialog);
        Window window = helpDialog.getWindow();
        window.requestFeature(Window.FEATURE_NO_TITLE);
        window.setGravity(Gravity.RIGHT | Gravity.TOP);
        window.setDimAmount(0f);
        helpDialog.setContentView(R.layout.dialog_pop_help);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.x = dip2px(this, 27);
        lp.y = dip2px(this, 39);
        TextView tvHelp = helpDialog.findViewById(R.id.tv_help);
//        tvHelp.setText("\n1.  单笔盈利额排名前十的用户可以上榜，榜单定时更新\n\n" +
//                "2.  点点盈利榜统计范围包含使用体验券订货以及所有在线产品\n\n" +
//                "3.  上榜用户均会给予相应的体验券奖励，奖励会在每个结算周期的后一天自动发到用户卡包内\n");
        tvHelp.setText(helpContent+"\n");
        helpDialog.show();
    }


    public static int dip2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    @OnClick(R2.id.tv_my_follow)
    public void onFollowClicked() {
        AppRouter.routeToMyFollow(this);
    }

    public class FragmentAdapter extends FragmentPagerAdapter {
        public FragmentAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            return fragments.get(i);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }
    }
}
